/**
 * shenxw
 */

function Play(type, foreignId) {

	this.url = "";
	this.userId = "";
	this.type = type;
	this.vasInfo = "";
	this.platform = "";
	this.epgDomain = "";
	this.pageUrl = "";
	this.backUrl = "";
	this.leftVal = "";
	this.epgHost = "";
	this.topVal = "";
	this.widthVal = "";
	this.heightVal = "";
	this.foreignId = foreignId;
	this.authUrl = "http://10.255.247.124:8080/serve/api/buyClient/serviceAuth.do";
	this.authBackUrl = "";
	this.vodTrailerPage = "";
	this.loggerFlag = -1;
	this.channelJson = null;
	this.channelParamName = "hwcode";
	this.groupInfo = {
		"hw" : {
			"target" : "gdhdpublic",
			"group" : [ "ZJTELECOMG500country", "ZJTELECOMSchoolHD",
					"gdgexinghua" ]
		},
		"zte" : {
			"target" : "frame1030",
			"group" : [ "frame1042", "frame1051", "frame1052" ]
		}
	};

	this.init = function() {
		//if (this.loggerFlag != -1) {
		//	this.showLoggerLayer();
		//}
		this.debug("pageUrl:  " + this.pageUrl);
		this.initPlatform();
		this.initVasInfo();
		this.initEpgDomain();
	};

	this.initPlatform = function() {
		this.platform = "hw";
		if (this.pageUrl.indexOf("frame") != -1) {
			this.platform = "zte";
			//this.channelParamName = "ztecode";
		}
	};

	this.initEpgDomain = function() {
		if (this.platform == "hw") {
			this.fix = "/en";
		} else {
			this.fix = "/vis_to_page.jsp";
		}
		this.groupHandler();
		this.epgDomain = this.pageUrl.substring(0, this.pageUrl
				.indexOf(this.fix));
		this.debug("epgDomain:  " + this.epgDomain);
	};

	this.groupHandler = function() {
		this.reg = "";
		this.array = this.groupInfo[this.platform].group;
		if(this.isEmpty(this.array)) {
			this.array = this.getGroup();
		}
		for (var i = 0; i < this.array.length; i++) {
			if (this.pageUrl.indexOf(this.array[i]) != -1) {
				this.reg = this.array[i];
			}
		}
		if (this.reg != "") {
			this.replaceGroup(this.reg);
		}
	};
	
	this.replaceGroup = function(reg) {
		var start = this.pageUrl.substring(0, this.pageUrl.indexOf(reg));
		var end = this.pageUrl
				.substring(this.pageUrl.indexOf(reg) + reg.length);
		var target = this.groupInfo[this.platform].target;
		if(this.isEmpty(target)) {
			target = this.getTarget();
		}
		this.pageUrl = start + target +  end;
	};
	
	this.getGroup = function() {
		var group = this.groupInfo.hw.group;
		if(this.platform == "zte") {
			group = this.groupInfo.zte.group;
		}
		return group;
	};
	
	this.getTarget = function() {
		var group = this.groupInfo.hw.target;
		if(this.platform == "zte") {
			group = this.groupInfo.zte.target;
		}
		return group;
	};

	this.initVasInfo = function() {
		if (this.type == "play_vod") {
			this.vasInfo = "<vas_action>play_vod</vas_action><mediacode>"
					+ this.foreignId + "</mediacode>"
					+ "<mediatype>VOD</mediatype>" + "<vas_back_url>"
					+ this.backUrl + "</vas_back_url>";
		} else if (this.type == "play_live") {
			//this.foreignId = this.getChannnelNum();
			this.vasInfo = "<vas_action>play_live</vas_action><userChannelID>"
					+ this.foreignId + "</userChannelID>" + "<vas_back_url>"
					+ this.backUrl + "</vas_back_url>";
		}

	};

	this.loadPlayUrl = function() {
		if (this.platform == "hw") {
			this.url = this.epgDomain + "/en/vis_to_page.jsp";
		} else {
			this.url = this.epgDomain + "/vis_to_page.jsp";
		}
	};

	this.loadVodTraielrPlayUrl = function() {
		if (this.platform == "hw") {
			this.url = this.epgDomain + "/en/play/vas_play_code.jsp";
		} else {
			this.url = this.epgDomain + "/play/vas_play_code.jsp";
		}
	};

	this.gotoPlay = function() {
		this.loadPlayUrl();
		this.url += "?vas_info=" + encodeURIComponent(this.vasInfo);
		this.debug("url:  " + this.url);
		this.jumpLink(this.url);
	};

	this.vodTrailerPlay = function() {
		this.trailerPage = this.vodTrailerPage + "?foreignId=" + this.foreignId
				+ "&left=" + this.leftVal + "&top=" + this.topVal + "&width="
				+ this.widthVal + "&height=" + this.heightVal;
		this.loadVodTraielrPlayUrl();
		this.url += "?mediacode=" + this.foreignId + "&vas_back_url="
				+ encodeURIComponent(this.trailerPage);
		this.debug("url:  " + this.url);
		this.jumpLink(this.url);
	};

	this.jumpLink = function(url) {
		window.location.href = url;
	};

	this.getAuthenticationAttr = function() {
		if ((typeof Authentication) != "undefined") {
			return Authentication.CTCGetConfig(name);
		} else {
			return "---";
		}
	};

	this.getChannnelNum = function() {
		if (this.isEmpty(this.channelJson)) {
			return "";
		}
		this.debug(this.channelParamName);
		for (var i = 0; i < this.channelJson.length; i++) {
			// this.debug(JSON.stringify((this.channelJson[i])));
			if (this.channelJson[i].name == this.channelParamName) {
				this.debug(this.channelJson[i].value);
				return this.channelJson[i].value;
				break;
			}
		}
		return "";
	};

	this.debug = function(msg) {
		if (this.loggerFlag == -1) {
			return;
		}
		document.getElementById("debug").innerHTML = document
				.getElementById("debug").innerHTML
				+ " <hr /> " + msg;
	};

	this.showLoggerLayer = function() {
		document.getElementById("debug").style.display = "block";
	};

	this.isEmpty = function(obj) {
		if (obj == "" || obj == null || (typeof obj) == "undefined") {
			return true;
		}
		return false;
	};

}